<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList"%>
<%@ page import="javax.jdo.PersistenceManager" %>
<%@ page import="com.google.appengine.api.users.User" %>
<%@ page import="com.google.appengine.api.users.UserService" %>
<%@ page import="com.google.appengine.api.users.UserServiceFactory" %>
<%@ page import="appnode.Node" %>
<%@ page import="appnode.SearchNode" %>
<%@ page import="appnode.Interface" %>
<%@ page import="appnode.Link" %>
<%@ page import="appnode.Linktools" %>
<%@ page import="appnode.FreqTools" %>
<%@ page import="appnode.PMF" %>
<%@ page import="java.util.Date"%>
<%@ page import="com.google.appengine.api.datastore.Key"%>
<%@ page import="com.google.appengine.api.datastore.KeyFactory;"%>


<html>
	<head>
		<link rel="stylesheet" href="map.css" type="text/css" media="screen" title="mapcss"></link>
		<title>Add a link</title>
		<script>
		function checkform() {
			valid=true;
			if (document.input.freq.value=="" || document.input.protocol.value==""){
				alert("Fill all fields");
				valid=false;	
			}
			else {
				var a = document.input.freq.value;
				var b = parseFloat(a);
				if (b<5480 || b>5720) {
					alert("The frequency is out of range");
					valid=false;
				}
			}
		return valid;
		}
		</script>
	</head>
	<%
		PersistenceManager pm = PMF.get().getPersistenceManager();	
		String nodename1 = request.getParameter("nodename1");
		String nodename2 = request.getParameter("nodename2");
		Key key = KeyFactory.stringToKey(nodename1);
		Node node1 = pm.getObjectById(Node.class,key);
		Node node2 = SearchNode.getByName(nodename2);
		int i = 0;
	%>
	<body class="window">
		<div class="openwindow">
		Select interfaces for new link<p>
		
		<form name="input" action="/addlink" method="get" onsubmit="return checkform()">
		<input type="hidden" name="action" value="add"></input>
		Node : <%out.print(node1.getNodename());%>
		<br>
		Interface : <select name="keystrif1">
		<%
			for (i=0;i<node1.getInterfaces().size();i++) {
				if (node1.getInterfaces().get(i).getLink()==null || node1.getInterfaces().get(i).getLink().equals("")) {
		%>
			<option value="<%out.print(node1.getInterfaces().get(i).getKeyStr());%>"><%out.print(node1.getInterfaces().get(i).getName());%></optional>
		<%
				}	
			} 
		%>
		</select><p>
			
		Node : <%out.print(nodename2);%>
		<br>
		Interface : <select name="keystrif2">
		<%
			for (i=0;i<node2.getInterfaces().size();i++) {
				if (node2.getInterfaces().get(i).getLink()==null || node2.getInterfaces().get(i).getLink().equals("")) {
		%>
			<option value="<%out.print(node2.getInterfaces().get(i).getKeyStr());%>"><%out.print(node2.getInterfaces().get(i).getName());%></optional>
		<%
				}
			} 
		%>
		</select><p>	
			
		
		Protocol : <select name="protocol"><option value="802.11a">802.11a</optional></select><p>	
		Frequency (5480-5720): <input type="text" name="freq"/><br>
		<%FreqTools listfreq = new FreqTools();%>
		There are <%=listfreq.listfreq(node1.getNodename(),node2.getNodename()).size()%> links close to your new link<br>
		<%=listfreq.listfreq(node1.getNodename(),node2.getNodename())%><p>
		<input type="submit" value="New link"/>
		<input type='button' value='Cancel' onClick='window.close();'/>
			
		</form>
		
		</div>
	</body>


<script>
function windowclose() {
	window.opener.location.reload();
	window.close();
}
</script>
</html>